/*******************************************************************************
 * Copyright 2008 www.softao.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
/**
 * @(#)SubmitButton.java Jul 19, 2008
 *
 * Placeholder for copyright, needs to be replaced before release.
 */
package com.softao.ming.gwt.user.client;

import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;

/**
 * This is implemented as default button, which is supposed to be put in the
 * <code>FormPanel</code>.
 * <p>
 * This button is used in the {@link FormPanel}, otherwise, it's not defaulted.
 * And also, needs to prevent the submit action of {@link FormPanel} while you
 * are trying to do anything on the <code>onClick</code> event handler of this
 * button.
 * </p>
 * 
 * <pre>
 * this.pnlForm.addFormHandler(new FormHandler() {
 *     public void onSubmit(FormSubmitEvent event) {
 *         event.setCancelled(true);
 *     }
 *
 *     public void onSubmitComplete(FormSubmitCompleteEvent event) {
 *     }
 * });
 * </pre>
 */
public class SubmitButton extends HTML {
	private static final String HTML_TEXT = "<input type=\"submit\" class=\"ming-submitButton\" value=\"{0}\"/>";

	/**
	 * Initializes a new instance of <code>SubmitButton</code>.
	 * 
	 * @param text
	 */
	public SubmitButton(String text) {
		this.setText(text);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.ui.HTML#setHTML(java.lang.String)
	 */
	@Override
	public void setHTML(String html) {
		// do nothing
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.ui.Label#setText(java.lang.String)
	 */
	@Override
	public void setText(String text) {
		super.setHTML(HTML_TEXT.replace("{0}", text));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.ui.Label#getText()
	 */
	@Override
	public String getText() {
		String html = this.getHTML();
		int index = html.indexOf("value=");
		String result = html.substring(index + 7, html.length() - 2);
		return result;
	}
}
